package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dao.DishMapper;
import com.itheima.pojo.Dish;
import com.itheima.dto.DishDto;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Transactional//需要同事保存两张表所以要进行事务管理
    public void saveWithFlaver(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }

    /*
     * 根据id查询菜品数据，用于回显*/
    public DishDto getByIdWithFlaver(Long id) {
        //查询菜品基本数据
        Dish dish = this.getById(id);
        //将dish的数据copy到dishDao
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //从DishFlavor中查询到口味的数据
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }


    /*
     * 修改菜品*/
    public void updateWithFlaver(DishDto dishDto) {
        //修改dish表中的数据
        this.updateById(dishDto);
        //从DishFlavor中查询菜品口味数据
        Long dishId = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> dfqw = new LambdaQueryWrapper<>();
        dfqw.eq(DishFlavor::getDishId, dishId);
        //删除菜品口味数据
        dishFlavorService.remove(dfqw);
        //添加菜品口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }

    /*
     * 批量删除+删除*/
    public void deleteIds(List<Long> ids) {

        //this.removeByIds(ids);


        //当售卖状态为停售时才可以删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        int count = this.count(dishLambdaQueryWrapper);
        if (count > 0) {
            //抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //可以删除
        this.removeByIds(ids);


        //删除口味表中的数据
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper);


    }
}
